package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.VoucherRecordQueryDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.domain.vo.VoucherRecordDO;
import com.recharge.service.VoucherRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 凭证核销记录 前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/voucher-record")
public class VoucherRecordController extends BaseController{

    @Autowired
    private VoucherRecordService voucherRecordService;

    /**
     * 添加凭证核销记录
     * @param domain
     * @return ObjectResponse
     */
    @RequestMapping(value = "/addVoucher.html",method = RequestMethod.POST)
    @ApiOperation(value = "添加凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse addVoucher(@RequestBody VoucherRecordDO domain){
        ObjectResponse<Object> response = new ObjectResponse<>();
        //获取操作"添加凭证核销记录"的用户
        UserLoginDO userLoginInfo = getUserLoginInfo();
        //将操作用户添加的凭证核销记录
        domain.setCreator(userLoginInfo.getUsername());
        ResultDO result = voucherRecordService.add(domain);
        if(!result.isSuccess()) {
            //添加凭证核销记录不成功
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return  response;
    }
    /**
     * 编辑凭证核销记录
     * @param domain
     * @return ObjectResponse
     */
    @RequestMapping(value = "/editVoucher.html",method = RequestMethod.POST)
    @ApiOperation(value = "编辑凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse editVoucher(@RequestBody VoucherRecordDO domain){
        ObjectResponse  response = new ObjectResponse<>();
        //从token获取编辑凭证核销记录的用户信息
        UserLoginDO userLoginDO = getUserLoginInfo();
        //将用户的用户名存入编辑凭证核销记录中
        domain.setCreator(userLoginDO.getUsername());
        //调用具体的业务操作
        ResultDO result = voucherRecordService.edit(domain);
        if(!result.isSuccess()) {
            //操作异常
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return  response;
    }
    /**
     * 返回凭证核销记录
     * @param domain
     * @return ResultDO
     */
    @RequestMapping(value = "/get.html",method = RequestMethod.POST)
    @ApiOperation(value = "返回凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse get(@RequestBody VoucherRecordDO domain){
        ObjectResponse<VoucherRecordDO> response = new ObjectResponse<>();
        ResultDO result = voucherRecordService.get(domain.getId());
        if(!result.isSuccess()) {
            //查询异常返回
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            try {
                //如果isSuccess返回ture，说明查询成功。将result里面的数据转存入response返回
                response.setData((VoucherRecordDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  response;
    }

    /**
     * 查找凭证核销记录
     * @param query
     * @return ResultDO
     */
    @RequestMapping(value = "/find.html",method = RequestMethod.POST)
    @ApiOperation(value = "查找凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse<PageDO> find(@RequestBody VoucherRecordQueryDO query){
        ObjectResponse<PageDO> response = new ObjectResponse<>();
        ResultDO result = voucherRecordService.find(query);
        if(!result.isSuccess()){
            //返回异常
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }else {
            //正常返回结果从result转存response
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    /**
     * 根据订单号查找凭证核销记录
     * @param domain
     * @return ObjectResponse
     */
    @RequestMapping(value = "/queryByOrderNo.html",method = RequestMethod.POST)
    @ApiOperation(value = "根据订单号查找凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse<List<VoucherRecordDO>> queryByOrderNo(@RequestBody VoucherRecordQueryDO domain){
        ObjectResponse<List<VoucherRecordDO>> response = new ObjectResponse<>();
        ResultDO result = voucherRecordService.findByOrderNo(domain);
        if(!result.isSuccess()){
            //返回异常
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }else {
            //正常返回结果从result转存response
            response.setData((List<VoucherRecordDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return  response;
    }
    /**
     * 根据供应单号查找凭证核销记录
     * @param domain
     * @return ResultDO
     */
    @RequestMapping(value = "/queryByProviderOrderNo.html",method = RequestMethod.POST)
    @ApiOperation(value = "根据供应单号查找凭证核销记录",notes = "")
    public @ResponseBody
    ObjectResponse<List<VoucherRecordDO>> queryByProviderOrderNo(@RequestBody VoucherRecordQueryDO domain){
        ObjectResponse<List<VoucherRecordDO>> response = new ObjectResponse<>();
        ResultDO result = voucherRecordService.findByProviderOrderNo(domain);
        if(!result.isSuccess()){
            //返回异常
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }else {
            //正常返回结果从result转存response
            response.setData((List<VoucherRecordDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return  response;
    }

}

